package com.ys.controller.web.business;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.bo.vo.W;
import com.ys.entity.business.TpAplusFile;
import com.ys.entity.business.TpBoutiqueSiteDetail;
import com.ys.entity.business.TpBoutiqueSiteFile;
import com.ys.service.sys.CommonApiService;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * 精品库站点详情页面
 */
@Controller
@RequestMapping("/web/business/tpBoutiqueSiteDetail")
public class WebTpBoutiqueSiteDetailController {
	private static final Logger logger = LoggerFactory.getLogger(WebTpBoutiqueSiteDetailController.class);

	public static Long MENU_ID = 10176L;
	public static Long MENU_ID_DETAIL = 10211L;
	public static Long MENU_ID_APLUS = 10177L;

	@Autowired
	private CommonApiService<TpBoutiqueSiteDetail> commonApiService;

	@Autowired
	private CommonApiService<TpAplusFile> commonApiAplusService;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response) {
		try {
			commonApiService.toAdd(model, new TpBoutiqueSiteDetail(),MENU_ID, null, null);
			model.addAttribute("onProcess", 0);
			return "business/tpBoutique/tpBoutiqueSiteDetailAdd";
		}catch (Exception e) {
			logger.error("精品库站点详情新增页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}

	public Map<String, Object> getExtraParam(Long id){
		Map<String, Object> extraParam = new TpBoutiqueSiteDetail()
				.addField("L.CategoryPath AS baseinfoLmName")
				.addField("D1.NameUnitCn AS normsUnit")
				.addField("D2.NameUnitCn AS weightUnit")
				.addLeftJoin("TB_BaseinfoLmwh L ON A.CommodityLmId = L.Id AND L.Status!=0")

				.addLeftJoin("TB_BaseinfoDwmc D1 ON A.NormsUnitId = D1.Id AND D1.Status!=0")
				.addLeftJoin("TB_BaseinfoDwmc D2 ON A.WeightUnitId = D2.Id AND D2.Status!=0")


				/*
				.addLeftJoin("TP_AmazonRegionItem I ON A.AmazonRegionItemId = I.Id")
				.addLeftJoin("TP_AmazonRegion R ON I.RegionCode = R.Code")
				.addLeftJoin("TB_Platform R1 ON R1.Code = R.PlatformCode")
				.addField("I.CountryName AS countryName")//站点
				.addField("R.Name AS regionName")//区域
				.addField("R.PlatformCode AS platformCode")//平台
				.addField("R1.Name platformName")
				 */
				.addLeftJoin("TB_Site S ON S.Id = A.SiteId")
				.addLeftJoin("TP_AmazonRegion AR ON AR.Code = S.RegionCode")
				.addField("AR.Name regionName")
				.addLeftJoin("TB_Platform B1 ON B1.Code = AR.PlatformCode")
				//.addField("B1.Code platform")
				.addField("B1.Name platform")
				.addLeftJoin("TB_Country C ON C.Id = S.CountryId")
				.addField("C.Name countryName")





				.setId(id)
				.queryFirstMap();
		return extraParam;
	}



	/*
	 * 编辑
	 */
	@GetMapping("/toEdit")
	public String toEdit(Model model, HttpServletRequest request, HttpServletResponse response,
			Long id) {
		try {
			//return commonApiService.toEdit(model, new TpBoutiqueSiteDetail(), MENU_ID, params);
			Map<String, Object> extraParam = getExtraParam(id);
			commonApiService.toEdit(model, id, new TpBoutiqueSiteDetail(), MENU_ID, extraParam, null);

			int aplusCount =  new TpAplusFile().setBoutiqueSiteDetailId(id).count();
			TpBoutiqueSiteFile tpBoutiqueSiteFile = new TpBoutiqueSiteFile().setBoutiqueSiteDetailId(id);
			int fileCount =  tpBoutiqueSiteFile.count();
			TpBoutiqueDetailEdit dto = new TpBoutiqueDetailEdit(id,aplusCount,fileCount);
            model.addAttribute("idInfo", dto);
			model.addAttribute("onProcess", 0);


			return "business/tpBoutique/tpBoutiqueSiteDetailEdit";
		}catch (Exception e) {
			logger.error("精品库站点详情编辑页面出错：",e.getMessage(),e);
				return W.error(model, e.getMessage());
		}
	}


	/*
	 * 详情
	 */
	@GetMapping("/toDetail")
	public String toDetail(Model model, HttpServletRequest request, HttpServletResponse response,
						 Long id) {
		try {
			//return commonApiService.toEdit(model, new TpBoutiqueSiteDetail(), MENU_ID, params);
			Map<String, Object> extraParam = getExtraParam(id);
			commonApiService.toEdit(model, id, new TpBoutiqueSiteDetail(), MENU_ID_DETAIL, extraParam, null);
			//int aplusCount = 0;
			TpAplusFile cond = new TpAplusFile().setBoutiqueSiteDetailId(id);
			cond.addField("O.CUser AS cUser");
			cond.addLeftJoin("THR_OperateCompanyApply O ON O.VGsBm = A.VgsBm");
			cond = commonApiAplusService.dataAuth(MENU_ID_APLUS, cond);//权限
			int aplusCount =  cond.count();
			TpBoutiqueSiteFile tpBoutiqueSiteFile = new TpBoutiqueSiteFile().setBoutiqueSiteDetailId(id);
			int fileCount =  tpBoutiqueSiteFile.count();
			TpBoutiqueDetailEdit dto = new TpBoutiqueDetailEdit(id,aplusCount,fileCount);
			model.addAttribute("idInfo", dto);
			model.addAttribute("onProcess", 1);
			return "business/tpBoutique/tpBoutiqueSiteDetailEdit";
		}catch (Exception e) {
			logger.error("精品库站点详情详情页面出错：",e.getMessage(),e);
			return W.error(model, e.getMessage());
		}
	}


	//A+管理类表
	@GetMapping("/aplusFileManage")
	public String aplusFileManage(Model model,String id,String onProcess){
		Map<String,Object> info = new TpBoutiqueSiteDetail()
				.addField("B.Code AS code")
				.addLeftJoin("TP_Boutique B ON B.Id = A.BoutiqueId")
				.setId(Long.parseLong(id))
				.queryFirstMap();
		if(info!=null){
			ToPageWithId dto = new ToPageWithId(Long.parseLong(info.get("boutiqueId").toString()),info.get("code").toString(),Long.parseLong(id));
			model.addAttribute("dto", dto);
			model.addAttribute("onProcess", onProcess);
		}

		return "business/tpAplusFile/tpAplusFileList";
	}
    @Data
    @AllArgsConstructor
    private static class TpBoutiqueDetailEdit{
        private Long id;
		private int aplusCount;
		private int fileCount;
    }

	@Data
	@AllArgsConstructor
	private static class ToPageWithId{
		private Long boutiqueId;//精品库Id
		private String code;//商品编码
		private Long boutiqueSiteDetailId;//精品库站点信息表Id
	}



	//附件信息表
	@GetMapping("/siteFileManage")
	public String siteFileManage(Model model,String id,String onProcess){
		Map<String,Object> info = new TpBoutiqueSiteDetail()
				.addField("B.Code AS code")
				.addLeftJoin("TP_Boutique B ON B.Id = A.BoutiqueId")
				.setId(Long.parseLong(id))
				.queryFirstMap();
		if(info!=null){
			ToPageWithId dto = new ToPageWithId(Long.parseLong(info.get("boutiqueId").toString()),info.get("code").toString(),Long.parseLong(id));
			model.addAttribute("dto", dto);
			model.addAttribute("onProcess", onProcess);
		}
		return "business/tpBoutique/tpBoutiqueSiteFile";
	}


	//批量维护商品数据
	/*
	@GetMapping("/importDetailList")
	public String importDetailList(Model model,String id){
		Map<String,Object> info = new TpBoutiqueSiteDetail()
				.addField("B.Code AS code")
				.addLeftJoin("TP_Boutique B ON B.Id = A.BoutiqueId")
				.setId(Long.parseLong(id))
				.queryFirstMap();
		if(info!=null){
			ToPageWithId dto = new ToPageWithId(Long.parseLong(info.get("boutiqueId").toString()),info.get("code").toString(),Long.parseLong(id));
			model.addAttribute("dto", dto);
		}
		return "business/tpBoutique/importDetailList";
	}

	 */

}






